package admin.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import admin.dao.AdminDAO;
import admin.model.AdminInfo;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
public class loginAction extends ActionSupport implements  Preparable, ModelDriven{
	
	AdminInfo adminInfo;
	private List<AdminInfo> list = new ArrayList<AdminInfo>();
	private AdminDAO adminDAO;
	
	
	@SuppressWarnings("unchecked")
	public String execute() throws Exception {
		ActionContext context = ActionContext.getContext();
		Map<String,String> session = (Map<String,String>)context.getSession();
		adminDAO = AdminDAO.getInstance();
		
		if(session.get("userId") == null || !"admin".equals(session.get("userId")) ){
			if(adminDAO.select(adminInfo) == null){
				return LOGIN;
			}
			
			String userid = adminInfo.getUserId();
			session.put("userId", userid );
			context.setSession(session);
		}
		return SUCCESS;
	}
	
	public String logOut() throws Exception{
		ActionContext context = ActionContext.getContext();
		Map<String,String> session = (Map<String,String>)context.getSession();
		if(session.get("userId") != null){
		   session.remove("userId");
		  }
		  context.setSession(session);
		 return SUCCESS;
	}

	public Object getModel() {
		return adminInfo;
	}
	public void prepare() throws Exception {
		adminInfo = new AdminInfo();
	}	
}
